﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.ComponentModel;

namespace produccion.clases
{
    class CLSDETUNI: INotifyPropertyChanged
    {
        MDOgeneral mod = new MDOgeneral();
        string coduni1_dun, coduni2_dun;
        double valor_dun;

        public string CODUNI1_DUN
        {
            get { return coduni1_dun; }
            set { coduni1_dun = value; }
        }

        public string CODUNI2_DUN
        {
            get { return coduni2_dun; }
            set { coduni2_dun = value; }
        }

        public double VALOR_DUN
        {
            get { return valor_dun; }
            set { valor_dun = value; }
        }

        public Boolean ExisteDetalle()
        {        
            try
            {
                string query = "select valor_dun from detunip where coduni1_dun='" + CODUNI1_DUN + "' and coduni2_dun='" + CODUNI2_DUN + "'";
                if (mod.consulta(query).Rows.Count > 0)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public string NombreUni2()
        {
            try
            {
                string query = "select nomuni_eun from encunip where coduni_eun='" + CODUNI2_DUN + "'";
                if (mod.consulta(query).Rows.Count > 0)
                {
                    return mod.consulta(query).Rows[0][0].ToString(); 
                }
                return mod.consulta(query).Rows[0][0].ToString(); 
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable ConsultarTodas()
        {
            try
            {
                string query = "select coduni1_dun, coduni2_dun, valor_dun from detunip order by coduni1_dun, coduni2_dun";
                DataTable dt = mod.consulta(query);
                if (dt.Rows.Count.Equals(0))
                {
                    query = "select '' as coduni1_dun, '' as coduni2_dun, 0 as valor_dun from detunip";
                    dt = mod.consulta(query);
                    return dt;
                }
                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable Consultar()
        {
            try
            {
                string query = " select CODUNI_EUN, NOMUNI_EUN,nvl(VALOR_DUN,0) as VALOR_DUN  from encunip left join detunip on CODUNI_EUN=CODUNI2_DUN  and CODUNI1_DUN='" + CODUNI1_DUN + "' where CODUNI_EUN<>'" + CODUNI1_DUN + "'";
                
                DataTable dt = mod.consulta(query);
                if (dt.Rows.Count > 0)
                {
                    return dt;
                }
                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int Eliminar()
        {
            try
            {
                string query = "delete from detunip where coduni1_dun='" + CODUNI1_DUN + "' or coduni2_dun='" + coduni1_dun + "'";
                return mod.comando(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int Actualizar()
        {
            try
            {
                string query = "update detunip set valor_dun=" + VALOR_DUN + " where coduni1_dun='" + CODUNI1_DUN + "' and coduni2_dun='" + CODUNI2_DUN + "'";
                return mod.comando(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int Insertar()
        {
            try
            {
                string query = "insert into detunip (coduni1_dun, coduni2_dun, valor_dun) values ('" + CODUNI1_DUN + "','" + CODUNI2_DUN + "'," + VALOR_DUN + ")";
                return mod.comando(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        public DataTable Buscar()
        {
            try
            {
                string query = "select coduni2_dun as codigo, valor_dun as valor from detunip where coduni1_dun like '%" + CODUNI1_DUN + "%' order by nomuni_eun asc";
                return mod.consulta(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
